/**
 * Created by szx on 2018-01-27.
 */
// JavaScript source code

var cxsContrls = angular.module('cxsContrls', []);//申明自定义模块

cxsContrls.controller('CxsList', ['$scope','$http', function ($scope,$http) {
    $scope.years = [];
    var date=new Date();
    $scope.selectedName=date.getFullYear();
    for(var i=0;i<10;i++){
        $scope.years.push($scope.selectedName-i);
    }


    //时间选择自动获取echart和table数据源
    $scope.selectChange=function () {
        $scope.GetDataSource();
    }

    $scope.legend=[];

    $scope.GetDataSource=function(){
        $.ajax({
            type: 'Get',
            url: './Data/tcy.json',
            data: {year:$scope.selectedName},
            dataType: 'json',
            contentType: "application/json",
            success: function (data) {
                $scope.$apply(function () {
                    $scope.echartData=data.echartData;
                    $scope.DataSoures=data.tableData;
                    $scope.TCYQS=data.TCYQS;
                    angular.forEach(data.TCYQS,function (item) {
                        $scope.legend.push(item.name);
                    });
                    

                    $scope.initChart();
                });
            }
        });
    }
    $scope.GetDataSource();

    //echart初始化
    $scope.initChart=function () {
        var myChart = echarts.init(document.getElementById('myChart2'));
        var option = {
            tooltip : {
                trigger: 'axis',
                axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                    type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            legend: {
                data: ['MTN', 'SCP','CP','PPN','PRN','ABN']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis:  {
                type: 'value'
            },
            yAxis: {
                type: 'category',
                data: $scope.echartData.CXS
            },
            series: [
                {
                    name: 'MTN',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true,
                            position: 'insideRight'
                        }
                    },
                    data: $scope.echartData.MTN
                },
                {
                    name: 'SCP',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true,
                            position: 'insideRight'
                        }
                    },
                    data: $scope.echartData.SCP
                },
                {
                    name: 'CP',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true,
                            position: 'insideRight'
                        }
                    },
                    data: $scope.echartData.CP
                },
                {
                    name: 'PPN',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true,
                            position: 'insideRight'
                        }
                    },
                    data: $scope.echartData.PPN
                },
                {
                    name: 'PRN',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true,
                            position: 'insideRight'
                        }
                    },
                    data: $scope.echartData.PRN
                },
                {
                    name: 'ABN',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true,
                            position: 'insideRight'
                        }
                    },
                    data: $scope.echartData.ABN
                }
            ]
        };
        myChart.setOption(option);
        //容器自适应
        window.onresize = myChart.resize;


        var option2 = {
            // title: {
            //     text: '折线图堆叠'
            // },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data:$scope.legend
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']
            },
            yAxis: {
                type: 'value'
            },
            series: $scope.TCYQS
        };

        var myChart2 = echarts.init(document.getElementById('tcyChart'));
        myChart2.setOption(option2);
        //容器自适应
        window.onresize = myChart2.resize;
    }

}]);